<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:fragment="head">
	<base th:href="@{/}">
	<meta charset="utf-8">
	<title>新增</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="static/layui/css/layui.css" media="all" />
	<link rel="stylesheet" type="text/css" href="static/jquery-easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="static/jquery-easyui/themes/icon.css">
	<link rel="stylesheet" href="static/layui/css/layui.css" media="all" />
	<script type="text/javascript" src="static/layui/layui.js"></script>
	<script type="text/javascript" src="static/jquery-easyui/jquery.min.js"></script>
	<script type="text/javascript" src="static/jquery-easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="static/jquery-easyui/locale/easyui-lang-zh_CN.js"></script> 
	<style type="text/css">
		.layui-form-label {
		    white-space: nowrap;
		    width: 120px;
		    color: gray;
		}
	</style>
</head>
<body class="layui-fluid" th:fragment="body">
	<div class="layui-row">
		<div class="layui-col-md12">
			<form class="easyui-form" id="form" action="" autocomplete="off" style="padding-top: 1px;" data-options="novalidate:true">
				<div class="layui-card-header">结算单表头信息</div>
				<div class="layui-form-item" style="margin-top: 20px;">
					<div class="layui-inline">
						<label class="layui-form-label">客户编码</label>
						<div class="layui-input-inline">
							<input name="customerCode" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">客户名称</label>
						<div class="layui-input-inline">
							<input name="customerName" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">客户订单号</label>
						<div class="layui-input-inline">
							<input name="customerOrder" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 20px;">
					<div class="layui-inline">
						<label class="layui-form-label">内部订单号</label>
						<div class="layui-input-inline">
							<input name="orderNo" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">销售订单号</label>
						<div class="layui-input-inline">
							<input name="saleOrderNo" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">关联订单号</label>
						<div class="layui-input-inline">
							<input name="relatedOrder" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 20px;">
					<div class="layui-inline">
						<label class="layui-form-label">承揽商</label>
						<div class="layui-input-inline">
							<input name="cls" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">产品类别</label>
						<div class="layui-input-inline">
							<input name="cplb" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">订单属性</label>
						<div class="layui-input-inline">
							<input name="ddsx" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 20px;">
					<div class="layui-inline">
						<label class="layui-form-label">关联订单号</label>
						<div class="layui-input-inline">
							<input name="relatedOrder" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">业务员</label>
						<div class="layui-input-inline">
							<input name="personName" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">创建时间</label>
						<div class="layui-input-inline">
							<input name="createTime" class="easyui-textbox" readonly="readonly">
						</div>
					</div>
				</div>
			</form>
			<div class="layui-card-header">结算单表体信息</div>
			<div class="layui-row" style="margin-top: 5px;">
				<div class="layui-col-md12">
					<table id="table" class="layui-table" lay-filter="table"></table>
				</div>
			</div>
			<div class="layui-card-header">订单工序信息</div>
			<div class="layui-row" style="margin-top: 5px;">
				<div class="layui-col-md12">
					<table id="processTable" class="layui-table" lay-filter="processTable"></table>
				</div>
			</div>
		</div>
	</div>
	<script type="text/html" id="processTableToolbar">
		<div class="layui-inline" shiro:hasPermission="btn:settlement_comfire">
<!--			<button class="layui-btn layui-btn-sm" lay-event="comfire"><i class="layui-icon">&#xe605;</i>确认结算价格</button>-->
			<button class="layui-btn layui-btn-sm" lay-event="lv"><i class="layui-icon">&#xe605;</i>按良率结算</button>
			<button class="layui-btn layui-btn-sm" lay-event="zd"><i class="layui-icon">&#xe605;</i>按整单结算</button>
		</div>
	</script>
	<script type="text/html" id="tableToolbar">
		<div class="layui-inline">
			<button class="layui-btn layui-btn-sm" lay-event="fix"><i class="layui-icon">&#xe65e;</i>发货情况</button>
		</div>
	</script>
	<script type="text/javascript" th:inline="javascript">
		layui.use(['layer','table','element','form'],function(){
			let layer = layui.layer,
				table = layui.table,
				form = layui.form,
				element = layui.element;

			let tableIns;
			let tableData;
			let selectId;

			$('#form').form('load','manage/settlement_order/info?masterId='+[[${id}]]);
			$('#form').form({
				onLoadSuccess: function(data){
					tableData = data.detailsList;
					table.render({
						elem:'#table'
						,page: true //开启分页
						,limit: 10
						,limits: [10,15,20,30,40,50,100]
						,loading: true
						,cols: [
							[ //表头
								{type:'radio'}
								,{field: 'cinvcode', title: '产品编码', minWidth:'150'}
								,{field: 'cinvname', title: '产品名称', minWidth:'150'}
								,{field: 'type', title: '类型', minWidth:'150'}
								,{field: 'settlementType', title: '结算类型', minWidth:'150', templet: function (d){
									return d.settlementType=='0'?'正常结算':'异常结算';
								}}
								,{field: 'zkv', title: '折扣率(%)', minWidth:'100'}
								,{field: 'settlementAmount', title: '折后金额', minWidth:'100'}
								,{field: 'cbatch', title: '批号', minWidth:'150'}
								,{field: 'qty', title: '订单数量', minWidth:'90'}
								,{field: 'sfsw', title: '是否实物', minWidth:'90'}
								,{field: 'sfll', title: '是否来料', minWidth:'90'}
								,{field: 'llrq', title: '来料日期', minWidth:'90'}
								,{field: 'czjslx', title: '产值计算类型', minWidth:'90'}
								,{field: 'tsbz', title: '特殊备注', minWidth:'90'}
							]
						]
						,text: {
							none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
						}
						,data: tableData
						,toolbar:'#tableToolbar'
						,defaultToolbar: []
						,done: function(res,curr,count){
							for(var i in res.data){
								if(!res.data[i].completFlag){
									$("tr[data-index='"+i+"']").attr({"style":"color:red","font-weight":"bold"})
								}
							}
						}
					});
				}
			});

			table.on('row(table)', function(obj){
				obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
				obj.tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
				form.render('radio');
				selectId = obj.data.id;
				$.post('manage/settlement_order/process_info',{detailsId: obj.data.id},function (d){
					tableIns = table.reload('processTable',{
						data: d
					})
				})
			});

			tableIns = table.render({
				elem:'#processTable'
				,page: true //开启分页
				,limit: 10
				,limits: [10,15,20,30,40,50,100]
				,loading: true
				,cols: [
					[ //表头
						{field: 'serialNumber', title: '序列号', minWidth:'80'}
						,{field: 'orderProcess', title: '订单工序', minWidth:'150'}
						,{field: 'orderProcessName', title: '工序名称', minWidth:'150'}
						,{field: 'ioritaxcost', title: '订单单价', minWidth:'90'}
						,{field: 'settlementcost', title: '结算单价', minWidth:'90',edit: true}
						,{field: 'sblx', title: '设备类型', minWidth:'90'}
						,{field: 'pricingMethod', title: '计价方式', minWidth:'150'}
						,{field: 'expectHours', title: '标准工时', minWidth:'150'}
						,{field: 'ioritaxcost', title: '价格', minWidth:'90'}
						,{field: 'qty', title: '合格数量', minWidth:'150'}
						,{field: 'unQty', title: '不合格数量', minWidth:'150'}
						,{field: 'jjfsje', title: '金额', minWidth:'120'}
					]
				]
				,text: {
					none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
				}
				,data: []
				,toolbar:'#processTableToolbar'
				,defaultToolbar: ['']
			});

			table.on('edit(processTable)', function(obj){
				let data = obj.data;
				let field = obj.field; // 得到修改的字段
				let value = obj.value // 得到修改后的值
				// 值的校验
				if(value.replace(/\s/g, '') === ''){
					layer.tips('值不能为空', this, {tips: 1});
					return obj.reedit(); // 重新编辑 -- v2.8.0 新增
				}
				// 更新当前缓存数据
				let update = {};
				if (field === 'settlementcost'){
					if (Number.parseFloat(value) <= 0){
						layer.tips('结算单价不能小于等于0', this, {tips: 1});
						return obj.reedit(); // 重新编辑 -- v2.8.0 新增
					}
					//重新计算计价方式金额
					if (data.pricingMethod==="计件"){
						data.jjfsje=data.qty*value
					}else if (data.pricingMethod==="计时"){
						data.jjfsje=data.qty*value*data.expectHours
					}else if (data.pricingMethod==="按批") {
						data.jjfsje = value
					}
					update['jjfsje'] = data.jjfsje;
				}
				update[field] = value;
				obj.update(update, true); // 参数 true 为 v2.7 新增功能，即同步更新其他包含自定义模板并可能存在关联的列视图
			});

			table.on('toolbar(processTable)', function(obj) {
				switch (obj.event) {
					case 'comfire':
						let datas = tableIns.config.data;
						if(datas.length === 0){
							layer.msg("请先选择一条结算单表体数据",{icon: '2'});
							return;
						}
						for(let i in datas){
							if(datas[i].ioritaxcost == null || datas[i].ioritaxcost === ''){
								layer.msg("销售订单还未定价，请先去销售订单确定价格",{icon: '2'});
								return;
							}
							if(datas[i].settlementcost == null || datas[i].settlementcost === ''){
								layer.msg("订单工序信息中第"+(++i)+"行数据的结算单价为空",{icon: '2'});
								return;
							}
						}
						$.post('manage/settlement_order/settlement_price_set',{settleDetailId: selectId,process: JSON.stringify(datas)},function (d){
							if(d.code == "200"){
								layer.msg("确认成功",{icon: '1'});
								for(var i in tableData){
									if(selectId == tableData[i].id){
										tableData[i].completFlag = true;
										break;
									}
								}
								table.reload('table',{
									data: tableData
								});
							}else{
								layer.alert("确认失败，"+d.msg,{icon: '2'});
							}
						})
						break;
					case 'lv':
						getdata('lv')
						break
					case 'zd':
						getdata('zd')
						break
				}
			})

			function getdata(type) {
				let datas = tableIns.config.data;
				if(datas.length === 0){
					layer.msg("请先选择一条结算单表体数据",{icon: '2'});
					return;
				}
				for(let i in datas){
					if(datas[i].ioritaxcost == null || datas[i].ioritaxcost === ''){
						layer.msg("销售订单还未定价，请先去销售订单确定价格",{icon: '2'});
						return;
					}
					if(datas[i].settlementcost == null || datas[i].settlementcost === ''){
						layer.msg("订单工序信息中第"+(++i)+"行数据的结算单价为空",{icon: '2'});
						return;
					}
				}
				$.post('manage/settlement_order/settlement_price_set',{settleDetailId: selectId,process: JSON.stringify(datas),type: type},function (d){
					if(d.code == "200"){
						layer.msg("确认成功",{icon: '1'});
						for(var i in tableData){
							if(selectId == tableData[i].id){
								tableData[i].completFlag = true;
								break;
							}
						}
						table.reload('table',{
							data: tableData
						});
					}else{
						layer.alert("确认失败，"+d.msg,{icon: '2'});
					}
				})
				return datas;
			}

			table.on('toolbar(table)', function(obj){
				switch (obj.event) {
					case "fix":
						top.layer.open({
							type: 2
							,title: "详情"
							,maxmin: true
							,area: ['100%', '80%']
							,content: '/manage/settlement_deliver_info/index/'+ $('input[name=orderNo]').val()
						});
						break;
				}
			})
		})
	</script>
</body>
</html>